package com.qf.fmall.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.fmall.index.service.IProductService;
import com.qf.fmall.product.entity.ProductParams;
import com.qf.fmall.product.service.IProductCommentsService;
import com.qf.fmall.product.service.IProductParamsService;
import com.qf.fmall.product.vo.CommentCountResponseVo;
import com.qf.fmall.product.vo.ProductCommentResponseVo;
import com.qf.fmall.product.vo.ProductInfoResponseVo;
import com.qf.fmall.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author djl
 * @since 2024-04-26
 */
@CrossOrigin
@RestController
@RequestMapping("/product")
@Slf4j   // Simple log facade
public class ProductController {

    @Autowired
    private IProductService productService;

    @Autowired
    private IProductParamsService productParamsService;

    @Autowired
    private IProductCommentsService productCommentsService;

    @GetMapping("/detail-commontscount/{id}")
    public ResultVo detailcommontscount(@PathVariable("id") Integer id){

        //1. 打印日志
        log.info("受到的参数为:id={}",id);

        //2. 校验参数
        if (id==null || id<0){
            throw new RuntimeException("缺少必传参数商品id");
        }

        //3. 调用servcie
        CommentCountResponseVo data = productCommentsService.detailcommontscount(id);

        //4. 返回json
        return new ResultVo(10000,"ok",data);
    }


    @GetMapping("/detail-commonts/{id}")
    public ResultVo detailCommonts(@PathVariable("id") Integer id,Integer pageNum,Integer limit){

        //1. 打印日志
        log.info("受到的参数为:id={},pageNum={},limit={}",id,pageNum,limit);

        //2. 校验参数
        if (id==null || id<0){
            throw new RuntimeException("缺少必传参数商品id");
        }

        if (pageNum<=0){
            throw new RuntimeException("当前页号非法");
        }
        if (limit<=0){
            throw new RuntimeException("页大小非法");
        }

        //3. 调用service
        ProductCommentResponseVo data= productCommentsService.detailCommonts(id,pageNum,limit);

        return new ResultVo(10000,"ok",data);

    }


    @GetMapping("/detail-params/{id}")
    public ResultVo detailParams(@PathVariable("id") Integer id ){
        //1. 打印日志
        log.info("接收到的参数为,id={}",id);

        //2. 检测参数
        if (id==null || id<0){
            throw new RuntimeException("缺少必传参数商品id");
        }

        //3. 调用servcie进行逻辑处理
        // select * from product_params where product_id=id
        ProductParams productParams = productParamsService.getOne(new QueryWrapper<ProductParams>().eq("product_id", id));

        //4. 返回json
        return new ResultVo(10000, "ok", productParams);
    }


    @GetMapping("/detail-info/{id}")
    public ResultVo detailInfo(@PathVariable("id") Integer id){

        //1. 打印日志
        log.info("接收到的参数为,id={}",id);

        //2. 检测参数
        if (id==null || id<0){
            throw new RuntimeException("缺少必传参数商品id");
        }

        //3. 调用servcie进行逻辑处理
        ProductInfoResponseVo data =  productService.detailInfo(id);

        //4. 返回json
        return new ResultVo(10000,"success",data);
    }

}
